import { department_select } from '@/services/department';
import { filedlist, permission_doctor_bind_platform } from '@/services/platformuser';
import { role_select } from '@/services/role';
import { ActionType, ProForm, ProFormSelect, ProFormTextArea } from '@ant-design/pro-components';
import { Divider, Form, message, Modal, TreeSelect } from 'antd';
import { useEffect, useState } from 'react';
interface Iprops {
  isShowAgreeModal: (show: boolean) => void;
  isAgreeModalOpen: boolean;
  id: number | undefined;
  actionRef2: React.MutableRefObject<ActionType | undefined>;
}
type T = {
  disabled: boolean;
  status: number;
  value: any;
  id: any;
  title: any;
  name: any;
  pId: any;
  parent_id: any;
};
type Val = {
  agree_reason: string;
  role_ids: any;
  department_id: string;
};
const Agree = (props: Iprops) => {
  const { isShowAgreeModal, isAgreeModalOpen, actionRef2, id } = props;
  // 存储科室列表
  const [departmentlist, setdepartmentlist] = useState();
  // 存储角色权限
  const [roleaAuthList, setRoleAuthList] = useState<any>(undefined);
  // 组件初始化渲染，获取科室列表与角色列表
  useEffect(() => {
    get_select_department();
    get_auth_list();
  }, []);
  // 获取科室列表
  const get_select_department = async () => {
    let params = await department_select();
    params.data?.map((item: T) => {
      item.disabled = item.status == 0 ? true : false;
      item.value = item.id;
      item.title = item.name;
      item.pId = item.parent_id;
    });
    setdepartmentlist(params.data);
  };
  // 获取角色权限列表
  const get_auth_list = async () => {
    let res: any = await role_select();
    console.log('res', res);
    if (res.code == 200) {
      res.data.map((item: any) => {
        item.value = item.id;
        item.label = item.rolename;
      });
      setRoleAuthList(res.data);
    }
  };
  // 获取职称
  const get_system_field = async (key: any) => {
    let request_body = {
      type: key,
    };
    let params: any = await filedlist(request_body);
    let res: any = [];
    params.data.map((item: { value: any; key: any }) => {
      let temp: any = {};
      temp.label = item.value;
      temp.value = item.key;
      res.push(temp);
    });
    return res;
  };
  // 确认同意
  const agree = async (value: Val) => {
    console.log('val', value);
    let request_body = {
      ...value,
      id,
      status: 1,
    };
    if (value.role_ids) {
      request_body.role_ids = JSON.stringify(value.role_ids.map((item: string) => item + ''));
    }
    let res: any = await permission_doctor_bind_platform(request_body);
    // console.log('res', res);
    if (res.msg == 'ok') {
      message.success('审批同意成功！');
      isShowAgreeModal(false);
      actionRef2.current?.reload();
    } else {
      message.error(res.msg);
    }
  };
  return (
    <Modal
      title="同意绑定"
      centered
      open={isAgreeModalOpen}
      onCancel={() => isShowAgreeModal(false)}
      destroyOnClose={true}
      width={500}
      footer={null}
    >
      <Divider />
      <ProForm
        onReset={() => isShowAgreeModal(false)}
        onFinish={(value: Val) => agree(value)}
        submitter={{
          // 配置按钮文本
          searchConfig: {
            resetText: '取消',
            submitText: '确定',
          },
          resetButtonProps: {
            style: {
              display: 'block',
              position: 'absolute',
              left: 'calc(50% - 107px)',
            },
          },
          submitButtonProps: {
            style: {
              display: 'block',
              position: 'absolute',
              right: 'calc(50% - 107px)',
            },
          },
        }}
      >
        <Form.Item name="department_id" label="所属科室">
          <TreeSelect
            treeDataSimpleMode
            treeData={departmentlist}
            placeholder="请选择科室"
            treeDefaultExpandAll
          />
        </Form.Item>
        <Form.Item name="role_ids" label="角色权限">
          <TreeSelect
            treeCheckable
            treeData={roleaAuthList}
            placeholder="请选择角色权限"
            treeDefaultExpandAll
          />
        </Form.Item>
        <ProFormSelect
          name="positional"
          label="职称"
          request={async () => get_system_field('doctor_positional')}
        ></ProFormSelect>
        <ProFormTextArea
          name="agree_reason"
          label="同意原因"
          placeholder="请填写同意原因"
          rules={[{ max: 100, message: '字数最大限制为100个字符' }]}
        />
        <br />
      </ProForm>
      <br />
    </Modal>
  );
};
export default Agree;
